<template>
	<view class="page padding-top-sm">
		<view class="info bg-white margin-lr margin-bottom-sm" v-if="doctor">
			<view class="title">就诊信息</view>
			<view class="flex justify-start align-center user">
				<image :src="$wanlshop.oss(doctor.image)" mode="aspectFill"></image>
				<view class="right">
					<view class="flex justify-start align-end">
						<view class="name">{{doctor.name}}</view>
						<text>{{doctor.doctor_level_text}} {{doctor.section_ids_text[0].name}}</text>
					</view>
					<text>{{doctor.hospitals && doctor.hospitals.title}}</text>
				</view>
			</view>
			
			<view class="list padding-top-sm">
				<view class="text">
					<text>就诊日期</text>
					<text>{{daytime}} {{timeList[time_type]}}</text>
				</view>
				<view class="text">
					<text>就诊科室</text>
					<text>{{doctor.section_ids_text[0].name}}</text>
				</view>
				<view class="text flex justify-between align-center">
					<view class="">
						<text>挂号费用</text>
						<text>￥{{doctor.registration_fee}} (余额：{{user.money.money1}})</text>
					</view>
					<text class="text-green chongzhi" @tap="$wanlshop.to(`/pages/user/money/recharge`)">充值</text>
				</view>
				<view class="text">
					<text>预约序号</text>
					<text>{{number}}号（实际以到院取号为准）</text>
				</view>
			</view>
		</view>
		
		<view class="info bg-white margin-lr margin-bottom-sm">
			<view class="flex justify-between align-center">
				<view class="title">就诊人信息</view>
				<!-- <view class="plus flex justify-start align-center text-green" @tap="$wanlshop.to('/pages/medicalcare/bookingsystem/add')">
					<text class="iconfont icon-add" style="font-size: 28rpx;margin-top: 6rpx;"></text>
					<text class="margin-left-xs text-s text-bold">添加就诊人</text>
				</view> -->
			</view>
			
			<u-radio-group class="w100" v-model="value" @change="radioGroupChange" v-if="visitor">
				<u-radio class="role w100" :name="visitor.id" active-color="#4CBD66">
					<view class="w100 flex justify-between align-center">
						<view class="top">
							<view class="name flex justify-start align-center">
								<text>{{visitor.name}}</text>
								<text class="tag text-green margin-left-sm">{{visitor.relation_txt}}</text>
							</view>
							<view class="text">{{visitor.sex_text}} {{visitor.age}}岁 {{visitor.mobile}}</view>
						</view>
						<!-- <text class="iconfont icon-xiugai text-green"></text> -->
					</view>
				</u-radio>
			</u-radio-group>
		</view>
		
		
		<view class="info bg-white margin-lr margin-bottom-lg" v-if="doctor">
			<view class="title">挂号须知</view>
			<view class="subtitle flex justify-start align-center">
				<text></text>
				<text>预约规则</text>
			</view>
			<view class="content">
				{{doctor.hospitals && doctor.hospitals.registr_notice}}
			</view>
		</view>
		<!-- 底部 -->
		<view class="wanlian cu-bar tabbar shop solid-top foot" v-if="scheduling">
			<view class="flex align-center">
				<text @tap="add_reservation" class="text-30 text-green">确认预约</text>
			</view>
		</view>
		<view class="safeAreaBottom" v-if="scheduling"></view>
		<u-modal :show-cancel-button="true" v-model="show" :content="content" @confirm="add_reservation" ref="uModal"></u-modal>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	export default {
		data() {
			return {
				show: false,
				content:"确认预约后，将自动从您的帐户余额扣除挂号费用，是否继续预约?",
				doctor: null,
				value: '',
				daytime: '',
				visitor:null,
				scheduling: null,
				doctor_id: '',
				visitor_id: '',
				loading: false,
				number: 0,
				time_type: 0,
				timeList:['','上午','下午']
			}
		},
		computed: {
			...mapState(['user'])
		},
		onNavigationBarButtonTap() {
			uni.switchTab({url:'/pages/index'})
		},
		onLoad(options) {
			this.daytime = options.daytime;
			this.doctor_id = options.id;
			this.visitor_id = options.visitor_id;
			this.time_type = options.time_type
			this.loadData(options.id,options.visitor_id);
		},
		methods: {
			async loadData(id,visitor_id) {
				await uni.request({
					url: '/addons/internethospital/api.doctor/detail',
					method: 'POST',
					data: {
						id: id,
					},
					success: res => {
						this.doctor = res.data;
					}
				});
				await uni.request({
					url: '/member/family/detail',
					method: 'POST',
					data: {
						id: visitor_id,
					},
					success: res => {
						this.visitor = res.data;
						this.value = res.data.id
					}
				});
				await uni.request({
					url: '/addons/internethospital/api.doctor/getschedulingid',
					method: 'POST',
					data: {
						doctor_id: id,
						days: this.daytime
					},
					success: res => {
						if(res.data.length > 0) {
							res.data.forEach((item,index) => {
								if(item.type == this.time_type || item.type == 3) {
									this.scheduling = item;
									console.log(this.scheduling)
									return false;
								}
							}) 
						}
					}
				});
				await uni.request({
					url: '/addons/internethospital/api.reservation/getlineupno',
					method: 'POST',
					data: {
						doctor_id: id,
						days: this.daytime
					},
					success: res => {
						this.number = res.data;
					}
				});
				
			},
			radioGroupChange(e) {
				
			},
			submit() {
				this.show = true;
			},
			async add_reservation() {
				if(this.loading) return false;
				if(!this.time_type) return this.$wanlshop.msg('请选择上午还是下午');
				this.loading = true
				await uni.request({
					url: '/addons/internethospital/api.reservation/add',
					method: 'POST',
					data: {
						visitor_id: this.visitor_id,
						doctor_id: this.doctor_id,
						scheduling_id: this.scheduling.id,
						time_type: this.time_type
					},
					success: res => {
						console.log(res)
						this.$wanlshop.msg(res.msg)
						setTimeout(() => {
							if(Number(this.doctor.registration_fee) == 0) {
								uni.redirectTo({
									url:`/pages/user/record/register/order`
								})
							} else {
								this.$wanlshop.to(`/pages/user/money/pay?type=reservation&id=${res.data.order_id}`);
							}
						}, 1500);
					},
					complete: () => {
						this.loading = false;
					}
				});
			}
		}
	}
</script>

<style lang="less">
page {
	background-color: #EDEDED;
}
.info {
	padding: 26rpx;
	border-radius: 10rpx;
	.title {
		height: 48rpx;
		font-size: 34rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #333333;
		line-height: 48rpx;
	}
	.user {
		margin-top: 28rpx;
		padding-bottom: 22rpx;
		border-bottom: 1rpx solid rgba(182,182,182,0.5);
		image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			margin-right: 22rpx;
		}
		.right {
			width: calc(100% - 102rpx);
			view {
				.name {
					font-size: 34rpx;
					font-family: PingFang SC-Bold, PingFang SC;
					font-weight: bold;
					color: #333333;
					line-height: 48rpx;
					margin-right: 12rpx;
				}
				text {
					display: inline-block;
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #333333;
					line-height: 40rpx;
				}
			}
			> text {
				display: inline-block;
				font-size: 24rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #999999;
				line-height: 34rpx;
				margin-top: 6rpx;
			}
		}
	}
	.list {
		.text {
			margin-top: 16rpx;
			text {
				display: inline-block;
				&:nth-child(1) {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #999999;
					line-height: 42rpx;
				}
				&:nth-child(2) {
					font-size: 28rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #333333;
					line-height: 42rpx;
					margin-left: 36rpx;
				}
			}
		}
	}
	
	.role {
		margin-top: 34rpx;
		/deep/.u-radio__label {
			width: 100%;
			margin-left: 20rpx;
			margin-right: 14rpx;
		}
		.top {
			.name {
				text {
					display: inline-block;
					&:nth-child(1) {
						font-size: 34rpx;
						font-family: PingFang SC-Bold, PingFang SC;
						font-weight: bold;
						color: #333333;
						line-height: 48rpx;
					}
					&:nth-child(2) {
						width: 54rpx;
						height: 26rpx;
						background: #E9F4EB;
						border-radius: 18rpx;
						font-size: 20rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #4CBD66;
						line-height: 26rpx;
						text-align: center;
					}
				}
			}
			.text {
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #666666;
				line-height: 40rpx;
			}
		}
		.iconfont {
			font-size: 32rpx;
		}
	}
	
	.subtitle {
		margin-top: 18rpx;
		text {
			display: inline-block;
			&:nth-child(1) {
				position: relative;
				width: 18rpx;
				height: 18rpx;
				background: rgba(76, 189, 102, 0.35);
				border-radius: 0rpx 0rpx 0rpx 0rpx;
				border-radius: 50%;
				margin: 0 14rpx;
				&::after {
					content: "";
					border-radius: 50%;
					display: inline-block;
					position: absolute;
					width: 10rpx;
					height: 10rpx;
					background: #4CBD66;
					z-index: 1;
					left: 4rpx;
					top: 4rpx;
				}
			}
			&:nth-child(2) {
				font-size: 30rpx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #666666;
				line-height: 42rpx;
			}
		}
	}
	.content {
		padding-left: 46rpx;
		margin-top: 14rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 400;
		color: #666666;
	}
}

.wanlian {
	background-color: #fff !important;
	> view {
		width: 100%;
		justify-content: space-around;
		> text {
			display: inline-block;
			width: 428rpx;
			height: 64rpx;
			background: #4CBD66;
			border-radius: 6rpx;
			font-size: 34rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 64rpx;
			text-align: center;
		}
	}
}
.wanlian.cu-bar.tabbar {
	height: calc(120rpx + env(safe-area-inset-bottom));
}
.chongzhi {
	color: #4CBD66 !important;
}
.list-box {
	.item {
		&:nth-child(1) {
			border-bottom: 1px solid #eee;
		}
	}
}
</style>
